$(function () {
    //选择店铺
    $(".cart-item").on("click", ".shop-yq-shop", function () {
        if ($(this).hasClass('checked')) {
            //选中
            $(this).removeClass('checked');
            $(this).parents('.cart-item').find(".g_yq").removeClass('checked');
            $('#shop_yq_all').removeClass('checked');
            $.ajax({
				type:'post',
				url:'/main/checkedcart',
				dataType:'json',
				data:{'productId':'all','action':'allremove'},
			    success:function(data){
			    },
			    error:function(){
			    }
			});
        } else {
            //未选中
            $(this).addClass('checked');
            $(this).parents('.cart-item').find(".g_yq").addClass('checked');
            $('#shop_yq_all').addClass('checked');
            $.ajax({
				type:'post',
				url:'/main/checkedcart',
				dataType:'json',
				data:{'productId':'all','action':'allchecked'},
			    success:function(data){
			    },
			    error:function(){
			    }
			});
        }

        $("#cart_pay_btn span").html($(".cart-item .g_yq.checked").length);
        $("#total_price span").html(calculate_price());

        $("#totalcount").val($(".cart-item .g_yq.checked").length);
        $("#totalamt").val(calculate_price());
    });

    //选择商品
    $(".cart-item").on("click", ".g_yq", function () {
        if ($(this).hasClass('checked')) {
            $(this).removeClass('checked');
            $(this).parents('.cart-item').find('.shop-yq-shop').removeClass('checked');
            $('#shop_yq_all').removeClass('checked');
            var proId = $(this).attr('data-proid');
            $.ajax({
				type:'post',
				url:'/main/checkedcart',
				dataType:'json',
				data:{'productId':proId,'action':'remove'},
			    success:function(data){
			    },
			    error:function(){
			    }
			});
            
        } else {
            $(this).addClass('checked');

            var length = $(this).parents('.cart-item').find('.g_yq').length;
            var checked_length = $(this).parents('.cart-item').find('.g_yq.checked').length;
            if (checked_length == length) {
                $(this).parents('.cart-item').find('.shop-yq-shop').addClass('checked');
                $('#shop_yq_all').addClass('checked');
            }
            var proId = $(this).attr('data-proid');
            $.ajax({
				type:'post',
				url:'/main/checkedcart',
				dataType:'json',
				data:{'productId':proId,'action':'add'},
			    success:function(data){
			    },
			    error:function(){
			    }
			});
        }
        $("#cart_pay_btn span").html($(".cart-item .g_yq.checked").length);
        $("#total_price span").html(calculate_price());
        
        $("#totalcount").val($(".cart-item .g_yq.checked").length);
        $("#totalamt").val(calculate_price());
    });

    //全选
    $('#shop_yq_all').on('click', function () {
        $(this).toggleClass('checked');
        $(".cart-item").find('.checkbox').toggleClass('checked');

        $.ajax({
			type:'post',
			url:'/main/checkedcart',
			dataType:'json',
			data:{'productId':'all','action':'allchecked'},
		    success:function(data){
		    },
		    error:function(){
		    }
		});
        
        $("#cart_pay_btn span").html($(".cart-item .g_yq.checked").length);
        $("#total_price span").html(calculate_price());
        
        $("#totalcount").val($(".cart-item .g_yq.checked").length);
        $("#totalamt").val(calculate_price());
    });

    //购物车编辑按钮
    var flag = 1;
    $(".o-title-shop .edit").on('click', function () {
        if (flag == 1) {
            $(this).parents('.cart-item').find('.item-del').css({'display': 'flex'});
            $(this).html('完成');
            flag = 0;
        } else {
            $(this).parents('.cart-item').find('.item-del').css({'display': 'none'});
            $(this).html('编辑');
            flag = 1;
        }
    });

    //购物车删除
    $('.o-item .item-del').on('click', function(){
        var $this = $(this);
        layer.open({
            content: '您确定要删除本商品吗？',
            btn: ['确定', '不要'],
            yes: function(index){
                layer.close(index);
                $this.parents('.cart-item').find('.edit').html('编辑');
                $this.parents('.cart-item').find('.item-del').css({'display':'none'});
                $this.parents('.o-item').remove();
                flag = 1;
                var proId = $this.attr('data-proid');
                $.ajax({
    				type:'post',
    				url:'/main/delCart',
    				dataType:'json',
    				data:{'productId':proId},
    			    success:function(data){
    			    },
    			    error:function(){
    			    }
    			});
                
                $("#cart_pay_btn span").html($(".cart-item .g_yq.checked").length);
                $("#total_price span").html(calculate_price());
                
                $("#totalcount").val($(".cart-item .g_yq.checked").length);
                $("#totalamt").val(calculate_price());
                
            }
        });
    });

});

//计算总价
function calculate_price() {
    var arr = $(".cart-item .g_yq.checked");
    var price = 0;
    var points = 0;
    for (var x = 0; x < arr.length; x++) {
        console.log("price-" + x + ":" + arr.eq(x).data('price'));
        var buy_num = parseInt(arr.eq(x).data("buy-num"));
        var point = parseInt(arr.eq(x).data("point"));
        points += point * buy_num;
        price += parseFloat(arr.eq(x).data('price')) * buy_num;
    }
    console.log("calculate:" + price);
    return points + '积分&nbsp;&nbsp;&yen;' + fmoney(price, 2);
}
function fmoney(s, n) {
    n = n > 0 && n <= 20 ? n : 2;
    s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
    var l = s.split(".")[0].split("").reverse(),
        r = s.split(".")[1];
    t = "";
    for (i = 0; i < l.length; i++) {
        t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
    }
    return t.split("").reverse().join("") + "." + r;
}